/* 这里写公告的接口 */

/* 
    发布公告，这个是往数据库中写数据 
    是管理员使用这个功能
*/

var express = require("express")
var router = express.Router()

/* 导入增删改查的方法 */
const { exec, getAll, getOne } = require("../../db")

/* 测试成功 */
/* 先是添加一条公告 */
router.post("/back/notice", async (req, res) => {
  /* 获取请求体中的数据 */
  /* 这个区域id是在前端的选择框选出来的，选择框中的值是区域名和电话，
  对应的value就是areainfo表中的id */
  /* 写这个公告的管理员的id,公告标题，公告内容，影响的区域的id */
  let { user_id, title, content, area_id } = req.body
  /* 编写sql语句，insert */
  let sql = `insert into noticeinfo (user_id,title,content,area_id) values('${user_id}','${title}','${content}','${area_id}')`
  try {
    let data = await exec(sql)
    sql = `SELECT n.id,n.user_id,n.title,n.content,n.submit_time,n.notice_status,a.area_name,a.tel from noticeinfo n  
    JOIN areainfo a on  n.area_id=a.id and n.id=${data.insertId}`
    data = await getOne(sql)
    res.send({
      code: 200,
      message: "success",
      result: data,
    })
  } catch (err) {
    console.log(err)
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

/* 测试成功 */
/* 然后是更改,点击某一条公告，就获取这个公告的id */
/* 更改就只是改变status的值，把这个status的值改为1 */
router.put("/back/notice/:id", async (req, res) => {
  /* 解析id值 */
  let { id } = req.params
  let sql = `update noticeinfo set notice_status=1 where id=${id}`
  try {
    await exec(sql)
    res.send({
      code: 200,
      message: "success",
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

/* 测试成功 */
/* 分页查询所有公告 */
router.get("/back/getallnotice", async (req, res) => {
  const { page, size } = req.query
  //获得总数
  let sql = `select count(*) as total from noticeinfo`
  let { total } = await getOne(sql)
  let offset = (page - 1) * size
  sql = `SELECT n.id,n.user_id,n.title,n.content,n.submit_time,n.notice_status,a.area_name,a.tel from noticeinfo n 
   JOIN areainfo a on  n.area_id=a.id order by submit_time desc LIMIT ${offset}, ${size}`
  try {
    let data = await getAll(sql)
    res.send({
      code: 200,
      message: "success",
      result: {
        total,
        data,
      },
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

/* 点击某一条公告查看这条公告的具体信息 */
router.get("/back/getonenotice/:id", async (req, res) => {
  let { id } = req.params
  let sql = `SELECT n.id,n.user_id,n.title,n.content,n.submit_time,n.notice_status,a.area_name,a.tel from noticeinfo n 
  JOIN areainfo a on  n.area_id=a.id and n.notice_status=0 and n.id='${id}'`
  try {
    let data = await getOne(sql)
    res.send({
      code: 200,
      message: "success",
      result: data,
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

// 查看最新一条公告
router.get("/notice/latest", async (req, res) => {
  let sql = `select max(submit_time) as submit_time,title,content from noticeinfo `
  try {
    let data = await getOne(sql)
    res.send({
      code: 200,
      message: "获取最新一条公告成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 201,
      message: "获取最新一条公告失败",
      result: data,
    })
  }
})
//查看所有公告(不分页)
router.get("/notice/getAll", async (req, res) => {
  let sql = `SELECT n.id,n.user_id,n.title,n.content,n.submit_time,n.notice_status,a.area_name,a.tel from noticeinfo n 
   JOIN areainfo a on  n.area_id=a.id where notice_status=0 order by submit_time desc `
  try {
    let data = await getAll(sql)
    res.send({
      code: 200,
      message: "获取所有公告成功",
      result: data
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "获取所有公告失败",
      result: "",
    })
  }
})
module.exports = router
